/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/3/14  lsy13  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Address;
import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.BookStore;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.service.BookStoreServer;
import com.sansi.pencilbook.service.UserServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 功能说明: 书铺控制器
 *
 * @author lsy13
 **/
@Slf4j
@RestController
@RequestMapping("/api/bookStore")
public class BookStoreController extends VerifyController implements BaseController{

  @Autowired
  private BookStoreServer bookStoreServer;

  @Autowired
  private UserServer userServer;

  @PostMapping("/u/addStore")
  public ResponseDO addStore(@RequestBody BookStore bookStore, HttpServletRequest request) {
    log.info("注册书铺");
    if (checkUser(request) || checkAdmin(request)) {
      Cookie[] cookies = request.getCookies();
      String userAccount = null;
      for (Cookie cookie : cookies) {
        if ("userAccount".equals(cookie.getName())) {
          userAccount = cookie.getValue();
        }
      }
      if (userAccount == null) {
        return ResponseDOUtil.setResponseDO(false, "用户账户不能为空");
      }
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Calendar calendar = Calendar.getInstance();
      Date date = calendar.getTime();
      bookStore.setStoreTime(new SimpleDateFormat("yyyyMMdd").format(date));
      bookStore.setUserId(user.getUserId());
      Result result = bookStoreServer.addStore(bookStore);
      bookStore = (BookStore) bookStoreServer.getStoreByUser(user.getUserId()).getData();
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), bookStore);
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @GetMapping("/u/getStoreId")
  public ResponseDO getStoreId(HttpServletRequest request) {
    log.info("获取书铺Id");
    if (checkUser(request) || checkAdmin(request)) {
      Cookie[] cookies = request.getCookies();
      String userAccount = null;
      for (Cookie cookie : cookies) {
        if ("userAccount".equals(cookie.getName())) {
          userAccount = cookie.getValue();
        }
      }
      if (userAccount == null) {
        return ResponseDOUtil.setResponseDO(false, "用户账户不能为空");
      }
      User user = (User) userServer.getUserByAccount(userAccount).getData();
      Result result = bookStoreServer.getStoreByUser(user.getUserId());
      BookStore bookStore = (BookStore) result.getData();
      return bookStore != null ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), bookStore.getStoreId()) :
          ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @GetMapping("/u/getStoreInfo")
  public ResponseDO getStoreInfo(HttpServletRequest request) {
    log.info("获取书铺信息");
    int storeId = 0;
    if ((storeId=getStore(request))!=0 || checkAdmin(request)) {
      Result result = bookStoreServer.getStore(storeId);
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), result.getData());
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @PostMapping("/u/updateStoreInfo")
  public ResponseDO updateStoreInfo(HttpServletRequest request, @RequestBody BookStore bookStore) {
    log.info("修改书铺信息");
    if (checkStore(request) || checkAdmin(request)) {
      Result result = bookStoreServer.modifyStore(bookStore);
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), result.getData());
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }
}
